package com.campus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.common.config.CodeConfig;
import com.campus.entity.Honor.HonorTemplate;
import com.campus.entity.ResultBody;
import com.campus.entity.System.SysUser;
import com.campus.mapper.HonorTemplateMapper;
import com.campus.service.HonorTemplateService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: AlaskaBoo
 * @Date: 2022-10-26 23:25
 **/
@RestController
@RequestMapping("/api/honorTemplate")
public class HonorTemplateController {
    @Autowired
    private HonorTemplateService honorTemplateService;
    @Autowired
    private HonorTemplateMapper honorTemplateMapper;

    /**
     * 查询ID
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Cacheable(value = "honorTemplate")
    public ResultBody getHonorTemplateListById(@PathVariable("id") Integer id) {
        HonorTemplate honorTemplate = honorTemplateService.getById(id);
        return ResultBody.ok().data("honorTemplate", honorTemplate);
    }

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param honorTemplate
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    @Cacheable(value = "honorTemplate")
    public ResultBody getAllHonorTemplateListPage(@PathVariable int currentPage, @PathVariable int pageSize, HonorTemplate honorTemplate) {
        Page<HonorTemplate> honorTemplatePage = new Page<>(currentPage, pageSize);
        IPage<HonorTemplate> honorTemplateInfo = honorTemplateService.honorTemplateList(honorTemplatePage, honorTemplate);
        if (honorTemplateInfo.getTotal() > 0) {
            return ResultBody.ok().data("honorTemplateInfo", honorTemplateInfo).message(CodeConfig.QUERY_SUCCESS.message);
        }
        return ResultBody.error().message(CodeConfig.QUERY_FAIL.message);
    }

    /**
     * 增加或者修改
     *
     * @param honorTemplate
     * @return
     */
    @PostMapping
    @CacheEvict(value = "honorTemplate", allEntries =true)
    public ResultBody saveHonorTemplate(@NotNull @RequestBody HonorTemplate honorTemplate) {
        if (honorTemplate.getId() == null || honorTemplate.getId() == -1) {
            SysUser user = new SysUser();
            honorTemplate.setCreateTime(new Date());
            honorTemplate.setCreateBy(String.valueOf(user.getId()));
            honorTemplateService.save(honorTemplate);
        } else {
            honorTemplate.setUpdateTime(new Date());
            honorTemplateService.updateById(honorTemplate);
        }
        return ResultBody.ok().message(CodeConfig.SUCCESS.message);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @CacheEvict(value = "honorTemplate", allEntries =true)
    public ResultBody delHonorTemplate(@PathVariable("id") Integer id) {
        honorTemplateService.removeById(id);
        return ResultBody.ok().message(CodeConfig.DELETE_SUCCESS.message);
    }
}
